package com.travel.config.factory;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author: libin
 * Date: 2025/6/20 8:24
 * Description:
 **/
@Slf4j
public class AsyncScheduleThreadFactory {


    /**
     * 1.定时任务的线程池创建出来
     */
    ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);

    /**
     * 单例模式的饿汉式
     * 饿汉式没有并发安全的问题。但是由于我是以前创建出来 所有可以浪费一些空间
     */
    public static final AsyncScheduleThreadFactory INSTANCE = new AsyncScheduleThreadFactory();

    /**
     * 私有一个构造器
     */

    private AsyncScheduleThreadFactory() {

    }


    /**
     * 2.返回提前创的实例对象
     * <p>
     * <p>
     * 这种方式：懒汉式存在并发修改问题
     *
     * @return
     */
    public static AsyncScheduleThreadFactory getInstance() {
//        AsyncScheduleThreadFactory asyncScheduleThreadFactory = new AsyncScheduleThreadFactory();
        return INSTANCE;
    }


    /**
     * 2.执行任务
     */
    public  void execute(Runnable runnable, Long ttl, TimeUnit timeUnit) {
        scheduledExecutorService.schedule(runnable, ttl, timeUnit);
    }


}

